package web{
	import down.Tools;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import morn.core.components.CheckBox;
	import morn.core.components.Label;
	import morn.core.components.LinkButton;
	import morn.core.handlers.Handler;
	import ui.down.WebUI;
	import utils.StringUtil;
	/**
	 * 1.没有弹框的美术文件
	 * 2.结果的测速完成在开始是什么状态？
	 * 3.测速中的滚动条是一张图片
	 * 网页测速
	 * @author SXL
	 */
	public class WebSpeed extends WebUI{
		private const coords:Array=[new Point(45, 125), new Point(320, 125), new Point(45, 155), new Point(320, 155)];
		private const regexp:RegExp=/^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-z][a-z0-9_]*)?$/;
		
		private var urlInfo:String;
		private var urlNames:Array;
		private var selfUrls:Array=[];
		private var bandInfo:Object;
		private static var _instance:WebSpeed;
		public static function get instance():WebSpeed{
            if (!_instance){
                _instance = new WebSpeed();
            }
            return _instance;
        }
		
		public function start():void{
			App.scene.scene=this;
			this.urlNames = ["www.baidu.com", "www.youku.com", "www.sina.com.cn", "www.taobao.com", "www.189.cn", "www.163.com"];
			
			this.addEventListener(MouseEvent.CLICK,this.onClick);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
			
			this.getUrl();
			this.initUrlList();
		}
		private function onClick(evt:MouseEvent):void{
			var name:String=evt.target.name;
			switch(name){
				case "btAdd":
					this.addUrl();
					break;
				case "btStart":
					this.startTest();
					break;
				case "tfDel1":
					WebAlert.instance.start("您确定要删除这个自定义网址？",new Handler(this.delUrl,[0]));
					break;
				case "tfDel2":
					WebAlert.instance.start("您确定要删除这个自定义网址？",new Handler(this.delUrl,[1]));
					break;
				case "tfDel3":
					WebAlert.instance.start("您确定要删除这个自定义网址？",new Handler(this.delUrl,[2]));
					break;
				case "tfDel4":
					WebAlert.instance.start("您确定要删除这个自定义网址？",new Handler(this.delUrl,[3]));
					break;
			}
		}
		private function startTest():void{
			for(var i:int=0;i<4;i++){
				var cb:CheckBox=this.getChildByName("cbUrl"+(i+1)) as CheckBox;
				if(!cb.visible||!cb.selected) continue;
				this.urlNames.push(this.selfUrls[i]);
			}
			WebResult.instance.start(this.urlNames,this.urlInfo,this.bandInfo);
		}
		/**删除网址*/
		private function delUrl(index:int):void{
			this.selfUrls.splice(index,1);
			this.initUrlList();
		}
		/**初始化自己添加的网址的显示列表*/
		private function initUrlList():void{
			var cb:CheckBox;
			var tf:LinkButton;
			for(var i:int=0;i<4;i++){
				cb=this.getChildByName("cbUrl"+(i+1)) as CheckBox;
				tf=this.getChildByName("tfDel"+(i+1)) as LinkButton;
				if(!this.selfUrls[i]){
					tf.visible=cb.visible=false;
					continue;
				}
				tf.visible=cb.visible=true;
				cb.label=this.selfUrls[i];
			}
		}
		private function addUrl():void{
			var str:String=StringUtil.trim(this.tfUrl.text);
			if(str==""){
				WebAlert.instance.start("您还没有输入网址!");
				return;
			}
			if (this.selfUrls.length>= 4){
                WebAlert.instance.start("自定义网址只能添加四个，请删除一个，再次添加。");
                return;
            }
			if (str.indexOf("/") != -1||!regexp.test(str)){
                 WebAlert.instance.start("此网址无效!");
                return;
            }
			this.selfUrls.push(str);
			this.initUrlList();
			this.tfUrl.text="";
		}
		/**游戏初始化获取一些服务器信息*/
		private function getUrl() : void{
            if (!ExternalInterface.available) return;
			
            var str:String = ExternalInterface.call("function getUrl(){return document.location.href;}");
            if (!str) return;
			
            str = str.substr(str.indexOf("?"), str.length);
            str = unescape(str);
            str = str.substr(6, str.length);
            this.urlInfo = str;
			this.bandInfo=Tools.strTOobj(str, ",", "=");
			this.bandInfo["server"] = Tools.strTOobj(this.bandInfo["server"], "#", "*");
        }
		private function onRemovedFromStage(evt:Event):void{
			this.removeEventListener(MouseEvent.CLICK,this.onClick);
		}
	}
}